$j=jQuery.noConflict();
$j(document).ready
(function(){
	$j("#firstcity").one("click",
	function(event){
		var city=$j(this).children().html();
		var cityRegion=$j("#region").children().html();
		$j.ajax(
		{
			url:"actions/chooseCityForm.php",
			data: "cityRegion="+cityRegion+"&city="+city,
			success: function(result)
			{
				$j("#firstcity").append(result);
				$j(".cityRes").attr("class", "firstCityRes");
				$j(".firstCityRes").css("display", "none");
				$j(".firstCityRes").slideDown(500);
				$j("#curCity").click(function()
				{
					$j(".firstCityRes").slideToggle(500);
				});
				$j(".aCityRes").one("click", function(event)
					{
						var val=$j(this).attr("val");
						var html=$j(this).html();
						$j(".firstCityRes").slideUp(500, function()
						{
							$j(".curCity").html(html);
							$j("#city").val(val);
							$j(".city").append('<input name="city" id="city" type="hidden" value="'+val+'">');
							if($j(".streetInput"))$j(".streetInput").val("");
							if($j(".buildingInput"))$j(".buildingInput").val("");

						});
				});

			}
		});
	});
	$j("#region").one("click",
	function(){
		var city=$j(".curCity").html();
		var region=$j(this).children().html();
		chooseRegion(region, city);
		});
});
function chooseCity(city, cityRegion)
	{
		$j.ajax(
		{
			url:"actions/chooseCityForm.php",
			data: "cityRegion="+cityRegion+"&city="+city,
			success: function(result)
			{
				$j("#city").append(result);
				$j(".cityRes").css("display", "none");
				$j(".cityRes").slideDown(500);
				$j(".curCity").click(function()
				{
					$j(".cityRes").slideToggle(500);
				});
				$j(".aCityRes").one("click", function(event)
				{					var val=$j(this).attr("val");
					var html=$j(this).html();
					$j(".cityRes").slideUp(500, function()
					{
						$j(".curCity").html(html);
						$j(".city").append('<input name="city" id="city" type="hidden" value="'+val+'">');
						$j("#city").val(val);
						if($j(".streetInput"))$j(".streetInput").val("");
						if($j(".buildingInput"))$j(".buildingInput").val("");

					});
				});
			}
		});
	}
	function chooseRegion(region, city)
	{
		$j("#firstcity").attr("id", "city");
		var check=false;
		$j.ajax
		({
			url:"actions/chooseCity.php",
			data: "region="+region,
			success: function(result)
			{
				var check=false;
				$j("#region").append(result);
				$j(".regionsRes").css("display", "none");
				$j(".regionsRes").slideDown(500);
				$j("#city").slideUp(500);
				$j("#city").html("<div class=\"curCity\">"+city+"</div>");
				$j(".regions").one("click", function(event)
				{
					region=$j(this).html();
					regionIsChosen(region);
					check=true;
				});
				if(!$j(".regions").html()=="") //если регион выбран
				{
					$j(".curRegion").click(function()
					{
						$j(".regions").slideToggle(500);
						$j(".curCity").slideDown(500);//css("display", "block");
						$j("#city").slideToggle(500);

					});
					$j("#city").one("click", function(event)
						{
							if(!check)
							chooseCity(city, region);
						});
				}
			}
		});

	}
	function regionIsChosen(region)
	{
		$j(".curRegion").html(region);
		$j(".regions").slideUp(500);
		$j(".regions").html("");
		$j(".curCity").html("Выберите город");
		$j("#city").slideDown(500);
		$j(".curRegion").one("click", function(event)
		{
			var region=$j(this).html();
			var city="Выберите город";
			chooseRegion(region, city);
		});
			$j("#city").one("click", function(event)
			{
				var city=$j(this).children().html();
				var cityRegion=$j("#region").children().html();
				chooseCity(city, cityRegion);
			});
	}
